
//# COMP 355    #  Sungho Park   10522678 sprakad@ust.hk
//# COMP 355    #  Hwaeium Yeom   10522862 hyeom@ust.hk

package pickMeOne.android;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.Random;



public class Pick extends Activity implements OnClickListener {
	/** Called when the activity is first created. */
	int numberOfOptions = 2;
	final int maxNumberOfOption = 5;
	final int minNumberOfOption = 2;
	String[] text = new String[maxNumberOfOption];
	EditText[] editText = new EditText[maxNumberOfOption];
	Spinner[] spinner = new Spinner[maxNumberOfOption];
	View pickButton;
	View addButton;
	View removeButton;
	String emptyString = new String();
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		super.onCreate(savedInstanceState);
		setContentView(R.layout.pick);
		pickButton = findViewById(R.id.pickButton);
		pickButton.setOnClickListener(this);
		addButton = findViewById(R.id.addButton);
		addButton.setOnClickListener(this);
		removeButton = findViewById(R.id.removeButton);
		removeButton.setOnClickListener(this);

		editText[0] = (EditText) findViewById(R.id.text1);
		editText[1] = (EditText) findViewById(R.id.text2);
		editText[2] = (EditText) findViewById(R.id.text3);
		editText[3] = (EditText) findViewById(R.id.text4);
		editText[4] = (EditText) findViewById(R.id.text5);
		editText[0].setOnClickListener(this);
		editText[1].setOnClickListener(this);
		editText[2].setOnClickListener(this);
		editText[3].setOnClickListener(this);
		editText[4].setOnClickListener(this);

		spinner[0] = (Spinner) findViewById(R.id.spinner1);
		spinner[1] = (Spinner) findViewById(R.id.spinner2);
		spinner[2] = (Spinner) findViewById(R.id.spinner3);
		spinner[3] = (Spinner) findViewById(R.id.spinner4);
		spinner[4] = (Spinner) findViewById(R.id.spinner5);
		for(int i=0; i<5; i++){
			spinner[i].setSelection(2);
		}
		emptyString = editText[0].getText().toString();
		
	}

	public String getAnswer() {
		int max = 0;
		Random rand = new Random();
		
		float[] ranResults = new float[numberOfOptions];
		
		for (int i = 0; i < numberOfOptions; i++) {
			ranResults[i] = rand.nextFloat() * Integer.parseInt(spinner[i].getSelectedItem().toString());
		} //end of random weight generation
		
		for ( int i= 0; i < numberOfOptions-1; i++){
			if (ranResults[i] >= ranResults[i+1])
				if(ranResults[i] >= ranResults[max]) // ith is the max
					max = i;
				else break;
			else if (ranResults[i+1] >= ranResults[max]){ //i+1th is the max
					max = i+1;
			}
			else break; //maxth is the max
				
		}//end of comparison
		return (text[max]);
		
	}//end of getAnswer()

	public void onClick(View v) {
		int flag =0;	//flag = 0 -> there i no empty edittext
		switch (v.getId()) {
		case R.id.pickButton:
			for(int i =0 ; i<numberOfOptions; i++){ //checking whether there is an empty edittext
				if (editText[i].getText().toString().compareTo(emptyString)==0){
					flag=1; //flag = 1 -> there is an empty edittext
				}
			}
			if(flag==1){
				AlertDialog alertDialog = new AlertDialog.Builder(Pick.this).create();
				alertDialog.setTitle("no option names...");
				alertDialog.setMessage("please fill the all fields");
				alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
					      // here you can add functions
					   }
					});
				alertDialog.show();
				break;	
			}
			else{
				for(int i =0 ; i<numberOfOptions;i++){
					text[i] = editText[i].getText().toString();				
				}
				String result = getAnswer();
				
				AlertDialog alertDialog = new AlertDialog.Builder(Pick.this).create();
				alertDialog.setTitle("go for");
				alertDialog.setMessage(result.toString());
				alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
					      // here you can add functions
					   }
					});
				alertDialog.show();
				break;	
			}
		case R.id.addButton:
			if (numberOfOptions == maxNumberOfOption){ // if the user try to make more than 5 options
				Toast toast=Toast.makeText(this, "the maximum number of options is 5", 70);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				break;
			}
			else {
				editText[numberOfOptions].setVisibility(0); //show one more edittext
				spinner[numberOfOptions].setVisibility(0); //show one more spinner
				numberOfOptions++;
				break;
			}
		case R.id.removeButton:
			if (numberOfOptions == minNumberOfOption){// if the user try to make less than 2 options
				Toast toast=Toast.makeText(this, "the minimum number of options is 2", 70);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				break;
			}
			else {
				numberOfOptions--;
				editText[numberOfOptions].setVisibility(8); //hide the last edittext
				spinner[numberOfOptions].setVisibility(8); //hide the last spinner
				break;
			}
		case R.id.text1:
			editText[0].setText("");
			break;
		case R.id.text2:
			editText[1].setText("");
			break;
		case R.id.text3:
			editText[2].setText("");
			break;
		case R.id.text4:
			editText[3].setText("");
			break;
		case R.id.text5:
			editText[4].setText("");
			break;
		}
	}

}